package com.magicsoft.share.ui.my;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseFragment;
import com.magicsoft.share.bean.my.MessageRead;
import com.magicsoft.share.bean.my.UserInfo;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.presenter.my.MyPresenter;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.view.my.IMyView;
import com.makeramen.roundedimageview.RoundedImageView;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import jp.wasabeef.glide.transformations.BlurTransformation;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends BaseFragment<IMyView, MyPresenter> implements IMyView {


    @BindView(R.id.backdrop)
    ImageView backdrop;
    @BindView(R.id.tv_share_num)
    TextView tvShareNum;
    @BindView(R.id.layout_share)
    LinearLayout layoutShare;
    @BindView(R.id.tv_collect_num)
    TextView tvCollectNum;
    @BindView(R.id.layout_collect)
    LinearLayout layoutCollect;
    @BindView(R.id.tv_follow_num)
    TextView tvFollowNum;
    @BindView(R.id.layout_follow)
    LinearLayout layoutFollow;
    @BindView(R.id.tv_fans_num)
    TextView tvFansNum;
    @BindView(R.id.layout_fans)
    LinearLayout layoutFans;
    @BindView(R.id.layout)
    LinearLayout layout;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_diamonds_golden)
    ImageView ivDiamondsGolden;
    @BindView(R.id.iv_head)
    RoundedImageView ivHead;
    @BindView(R.id.layout_gold)
    RelativeLayout layoutGold;
    @BindView(R.id.layout_vip)
    RelativeLayout layoutVip;
    @BindView(R.id.layout_order)
    RelativeLayout layoutOrder;
    @BindView(R.id.layout_msg)
    RelativeLayout layoutMsg;
    @BindView(R.id.layout_address)
    RelativeLayout layoutAddress;
    @BindView(R.id.layout_about)
    RelativeLayout layoutAbout;
    @BindView(R.id.layout_setting)
    RelativeLayout layoutSetting;
    Unbinder unbinder;
    @BindView(R.id.tv_balance_num)
    TextView tvBalanceNum;
    @BindView(R.id.tv_msg)
    TextView tvMsg;
    @BindView(R.id.view_notice)
    View viewNotice;
    Unbinder unbinder1;
    private UserInfo mUserInfo = new UserInfo();

    public MyFragment() {
        // Required empty public constructor
    }


    @Override
    public void onResume() {
        super.onResume();
        setImmersionBarEnabled(true);

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    public MyPresenter initPresenter() {
        return new MyPresenter();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void initView() {


    }

    @Override
    protected void requestData() {
        mPresenter.userDetail();
        mPresenter.messageRead();
    }

    @OnClick({R.id.iv_head, R.id.layout_share, R.id.layout_collect, R.id.layout_follow, R.id.layout_fans, R.id.layout_gold, R.id.layout_vip, R.id.layout_order, R.id.layout_msg, R.id.layout_address, R.id.layout_about, R.id.layout_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_head:

                startActivity(new Intent(getActivity(), PersonalInfoActivity.class));
                break;
            case R.id.layout_share:
                startActivity(new Intent(getActivity(), AllShareActivity.class));
                break;
            case R.id.layout_collect:
                startActivity(new Intent(getActivity(), CollectActivity.class));
                break;
            case R.id.layout_follow:
                startActivity(new Intent(getActivity(), FollowActivity.class));
                break;
            case R.id.layout_fans:
                startActivity(new Intent(getActivity(), FansActivity.class));
                break;
            case R.id.layout_gold:
                if (mUserInfo.getMember() == 1) {
                    Intent intent = new Intent(getActivity(), GoldActivity.class);
                    intent.putExtra("goldNum", mUserInfo.getBalance());
                    startActivity(intent);

                } else {
                    startActivity(new Intent(getActivity(), GoldNoVipActivity.class));
                }


                break;
            case R.id.layout_vip:
                if (mUserInfo.getMember() == 1) {
                    Intent intent = new Intent(getActivity(), VipActivity.class);
                    intent.putExtra("userInfo", mUserInfo);
                    startActivity(intent);

                } else {
                    startActivity(new Intent(getActivity(), VipNoActivity.class));
                }

                break;
            case R.id.layout_order:
                startActivity(new Intent(getActivity(), OrderActivity.class));
                break;
            case R.id.layout_msg:
                startActivity(new Intent(getActivity(), MessageActivity.class));
                break;
            case R.id.layout_address:
                startActivity(new Intent(getActivity(), AddressActivity.class));
                break;
            case R.id.layout_about:
                startActivity(new Intent(getActivity(), AboutActivity.class));
                break;
            case R.id.layout_setting:
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.ibtn_left:
                getActivity().finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void userDetailSuccess(UserInfo userInfo) {

        SPUtils.getInstance().put(SPKey.NAME,userInfo.getNickname());
        SPUtils.getInstance().put(SPKey.HEADIMG,userInfo.getHeadimg());

        mUserInfo = userInfo;
        if (userInfo.getMember() == 0) {
            tvId.setVisibility(View.GONE);
            ivDiamondsGolden.setVisibility(View.GONE);
        } else {
            tvId.setText("齐享号" + userInfo.getQixiang_no());
        }
        Glide.with(this).load(userInfo.getHeadimg()).into(ivHead);
        Glide.with(this).load(userInfo.getHeadimg())
                .apply(bitmapTransform(new BlurTransformation(22)))
                .into(backdrop);
        tvName.setText(userInfo.getNickname());
        tvSign.setText(userInfo.getPersonalized_signature());
        tvShareNum.setText(userInfo.getArticle_num());
        tvCollectNum.setText(userInfo.getCollection_num());
        tvFollowNum.setText(userInfo.getFollow_num());
        tvFansNum.setText(userInfo.getFans_num());
        tvBalanceNum.setText(userInfo.getBalance());


    }

    @Override
    public void messageRead(MessageRead data) {

        if (data.getAll_read_type() == 1) {
            viewNotice.setVisibility(View.VISIBLE);
        } else {
            viewNotice.setVisibility(View.GONE);
        }
    }


}
